package com.software.levelupandroid.systemTask.view.adapter;

import android.app.AlarmManager;
import android.app.DatePickerDialog;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.PorterDuff;
import android.icu.text.SimpleDateFormat;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.loopeer.cardstack.CardStackView;
import com.loopeer.cardstack.StackAdapter;
import com.software.levelupandroid.R;
import com.software.levelupandroid.entity.SystemTask;
import com.software.levelupandroid.entity.Task;
import com.software.levelupandroid.service.NotionService;
import com.software.levelupandroid.systemTask.presenter.TaskPresenter;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

public class TestStackAdapter extends StackAdapter<Integer> implements saveInterface{
    List<String> mList = new java.util.ArrayList<String>();
    AlarmManager iAlarmManager;

    List<SystemTask> tasks = new ArrayList<>();
    RadioButton radbtn;
    private TaskPresenter iTaskPresenter;
    int mType = 0;



    public TestStackAdapter(Context context, List<String> list,
                            AlarmManager alarmManager,TaskPresenter taskPresenter,int type) {
        super(context);
        mList = list;
        iAlarmManager = alarmManager;
        iTaskPresenter = taskPresenter;
        mType = type;
    }


    @Override
    public void bindView(Integer data, int position, CardStackView.ViewHolder holder) {

            ColorItemLargeHeaderViewHolder h = (ColorItemLargeHeaderViewHolder) holder;
            h.onBind(data, position);

    }


    @Override
    protected CardStackView.ViewHolder onCreateView(ViewGroup parent, int viewType) {
        View view;

        view = getLayoutInflater().inflate(R.layout.list_card_item, parent, false);
        return new ColorItemLargeHeaderViewHolder(view,mList);

    }

    @Override
    public List<SystemTask> btnSendMsg() {
        return tasks;
    }


    class ColorItemLargeHeaderViewHolder extends CardStackView.ViewHolder {
        View mLayout;
        View mContainerContent,mFrameContent;
        TextView mTextTitle, mTextContent;
        AlarmManager alarmManager;
        private PendingIntent pi;
        private TimePickerDialog timePickerDialog;
        private DatePickerDialog datePickerDialog;
        private Calendar calendar = Calendar.getInstance();
        List<String> list = new java.util.ArrayList<String>();
        private static final int DAY_IN_MILLIS = 86400000;

        public ColorItemLargeHeaderViewHolder(View view, List<String> mlist) {
            super(view);
            mLayout = view.findViewById(R.id.frame_list_card_item);
            mContainerContent = view.findViewById(R.id.container_list_content);
            mTextTitle = (TextView) view.findViewById(R.id.text_list_card_title);
            mTextContent = (TextView) view.findViewById(R.id.text_list_card_content);
            mFrameContent = view.findViewById(R.id.frame_list_card_item);


            list = mlist;
        }

        @Override
        public void onItemExpand(boolean b) {
            mContainerContent.setVisibility(b ? View.VISIBLE : View.GONE);

        }

        @Override
        protected void onAnimationStateChange(int state, boolean willBeSelect) {
            super.onAnimationStateChange(state, willBeSelect);
            if (state == CardStackView.ANIMATION_STATE_START && willBeSelect) {
                onItemExpand(true);
            }
            if (state == CardStackView.ANIMATION_STATE_END && !willBeSelect) {
                onItemExpand(false);
            }
        }

        public List<SystemTask> btnSendMsg() {
            return tasks;
        }
        public String InitSystemTaskDate(int year,int monthOfYear, int dayOfMonth) {
            String moy = (monthOfYear +1)+"";
            String day1 = dayOfMonth+"";
            if(monthOfYear+1 < 10){
                moy = "0"+moy;
            }
            if (dayOfMonth < 10){
                day1="0"+day1;
            }
            return String.valueOf(year) + "年" + moy + "月" + day1 + "日";
        }


        public void onBind(Integer data, int position) {
            Calendar currentTime = Calendar.getInstance();
            Calendar c = Calendar.getInstance();
            //卡片颜色的设置
            mLayout.getBackground().setColorFilter(ContextCompat.getColor(getContext(), data), PorterDuff.Mode.SRC_IN);
            mTextTitle.setText(list.get(position));
            Map<String, String> timeMap = new HashMap<String, String>();
            TextView timeText = (TextView) itemView.findViewById(R.id.text_list_card_content);
            TextView beginTime = (TextView) itemView.findViewById(R.id.text_list_card_begin_time);
            TextView endTime = (TextView) itemView.findViewById(R.id.text_list_card_end_time);



            //提交
            itemView.findViewById(R.id.text_list_card_comfirm).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //将内容装进其中
                    Date date = new Date();
                    SimpleDateFormat dateFormat1 = new SimpleDateFormat("HH:mm");
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");

                    /**新增系统任务入数据库*/
                    iTaskPresenter.insertSystemTask(mType,timeText.getText().toString(),
                            beginTime.getText().toString(),endTime.getText().toString(),getContext());


                    if (beginTime.getText().toString().equals(endTime.getText().toString())){
                        //单次任务
                        iAlarmManager.set(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), pi);
                    }else {
                        //每天重复任务
                        iAlarmManager.setRepeating(AlarmManager.RTC_WAKEUP, c.getTimeInMillis(), DAY_IN_MILLIS, pi);
                    }
                    Toast.makeText(getContext(), "Ok提交", Toast.LENGTH_SHORT).show();
                }
            });

            //设置提醒时间
            timeText.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    ((CardStackView) itemView.getParent()).performItemClick(ColorItemLargeHeaderViewHolder.this);
                    timePickerDialog = new TimePickerDialog(getContext(), new TimePickerDialog.OnTimeSetListener() {
                        @Override
                        public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
                            Intent intent = new Intent(getContext(),NotionService.class);
                            intent.putExtra("title","level up");
                            intent.putExtra("content", mList.get(position)+"");
                            intent.putExtra("task_type","3");

                            pi = PendingIntent.getService(getContext(), 0, intent, 0);
                            String day = hourOfDay+"";
                            String minute1 = minute+"";
                            if(hourOfDay < 10){
                                day = "0" + day;
                            }
                            if(minute < 10){
                                minute1 = "0" + minute1;
                            }
                            timeText.setText(day + ":" + minute1);
                            // 根据用户选择的时间来设置Calendar对象
                            c.set(Calendar.HOUR, hourOfDay);
                            c.set(Calendar.MINUTE, minute);
                        }
                    }, calendar.get(Calendar.HOUR_OF_DAY), calendar.get(Calendar.MINUTE), true);
                    timePickerDialog.show();
                    timePickerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                }
            });

            //设置开始时间
            beginTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    datePickerDialog = new DatePickerDialog(
                            getContext(), new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            //monthOfYear 得到的月份会减1所以我们要加1,initSystemTaskDate方法将字符格式化
                            String time = InitSystemTaskDate(year,monthOfYear,dayOfMonth);

                            ((TextView) itemView.findViewById(R.id.text_list_card_begin_time))
                                    .setText(time);
                        }
                    },
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH));
                    datePickerDialog.show();
                    //自动弹出键盘问题解决
                    datePickerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                }
            });

            //设置结束时间
            endTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    datePickerDialog = new DatePickerDialog(
                            getContext(), new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            //monthOfYear 得到的月份会减1所以我们要加1

                            String time = InitSystemTaskDate(year,monthOfYear,dayOfMonth);
                            Log.d("测试", time);
                            ((TextView) itemView.findViewById(R.id.text_list_card_end_time))
                                    .setText(time);
                        }
                    },
                            calendar.get(Calendar.YEAR),
                            calendar.get(Calendar.MONTH),
                            calendar.get(Calendar.DAY_OF_MONTH));
                    datePickerDialog.show();
                    //自动弹出键盘问题解决
                    datePickerDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
                }

            });


        }


    }

}
